import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class SharePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ShareTableModel model;
	private JTable shareTable;
	private JPanel leftPanel;
	private JButton shareButton;

	public SharePanel() {
		
		this.setLayout(new BorderLayout());
		leftPanel = new JPanel();
		leftPanel.setLayout(new GridBagLayout());
		model = new ShareTableModel();
		shareTable = new JTable(model);
		shareTable.setFillsViewportHeight(true);
		shareTable.setVisible(true);
		shareTable.setColumnSelectionAllowed(false);
		this.add(new JScrollPane(shareTable));
		// lewa strona
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.CENTER;
		c.gridx = 0;
		c.gridy = 0;
		c.gridy = 1;
		shareButton = new JButton("Add..");
		shareButton.setVisible(true);
		leftPanel.add(shareButton, c);
		leftPanel.setVisible(true);
		this.add(leftPanel, BorderLayout.WEST);
		//loadShared();
		
		shareButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				add();
				//System.out.println("click");
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
				
				
				
	
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});

	}
	
	public void add(){
		final JFileChooser fc = new JFileChooser();
		int returnVal = fc.showDialog(SharePanel.this, "Share");
		int addFlag = 0, breakFlag = 0; 
		File file =	fc.getSelectedFile();
		if(file != null) {	
		String data[][] = new String[100][2];
		data = (String[][])model.getData();
		
		
		if(data[0][0].compareTo("brak plikow") == 0) {
			data[0][0] = file.getPath();
			data[0][1] = ""+file.length();
			addFlag = 1;
			model.setData(data);
			Client.getMainFr().getProg().getOut().println(String.format("addFile;%s;%d", file.getPath(), file.length()));
			repaint();
		}
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(new File("settings/shared.cfg")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(addFlag == 0)
		for(int i=0; i<100; i++) {
			if(data[i][0] != null && data[i][0].compareTo(file.getPath()) == 0) {
				System.out.println("udostepniasz juz taki plik!");
				JOptionPane.showMessageDialog(null, "Udostepniasz juz taki plik!", "Uwaga! ", JOptionPane.INFORMATION_MESSAGE);
				breakFlag = 1;
				break;
			}
			
			if(data[i][0] == null && addFlag == 0) {
				String path = file.getPath();
				String size = ""+file.length();
				data[i][0] = path;
				data[i][1] = size;
				addFlag = 1;
				model.setData(data);
				Client.getMainFr().getProg().getOut().println(String.format("addFile;%s;%d", file.getPath(), file.length()));
				try {
					writer.write(path+";"+size);
					writer.newLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				repaint();
			}
			
		}
		try {
	 		writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(breakFlag == 1) {
			System.out.println("nie udalo sie");
		}
		
		}
	}
	
	public void loadShared() {
		Scanner scanner = null;
		File path = new File("settings");
		path.mkdirs();
		File file = new File(path+"/shared.cfg");
		try {
			file.createNewFile();
			scanner = new Scanner(new FileInputStream(file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(scanner != null){
			String buff[] ;
			String line;
			int n = 0;
			removeEl();
			Object [][]data = model.getData();
			while(scanner.hasNext()){
				//TODO Sprwadzic czy plik dalej istnieje w systemie pod ta sciezka
				line = scanner.nextLine();
				buff = line.split(";");
				System.out.println(buff[0]+"  "+buff[1]);
				if(new File(buff[0]).exists()){
					System.out.println("Plik: "+buff[0]+" istnieje.");
					data[n][0] = buff[0];
					data[n][1] = buff[1];
				}else System.out.println("Plik: "+buff[0]+" nie istnieje.");
				n++;
			}
			model.setData(data);
		}
		scanner.close();
	}
	
	public void removeEl() {
		for (int i = 0; i < 100; i++)
			for (int j = 0; j < 2; j++)
				model.getData()[i][j] = "";
		shareTable.repaint();
	}
}
